/*
 *  Licensed to PECATS under one or more contributor license agreements below:
 *  1. http://www.mozilla.org/MPL/MPL-1.1.html
 *  2. http://www.gnu.org/licenses/gpl-2.0.html
 *  3. http://www.gnu.org/licenses/lgpl-2.1.html
*/

/* **********************************************************
*  Class for Managing Customer & Account Information
*  Name: CustomerAccoutManager.java
*  Author: www.pecats.com
*  Date: September 15, 2008
************************************************************/

package com.pecats.server.core;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;

import com.pecats.server.util.HibernateUtil;
import com.pecats.server.core.Customer;
import com.pecats.server.core.Account;

/**
 * Class used to handle database operations with the help of Hibernate from the incoming Servlet Request.
 */

public class CustomerAccountManager 
{

	public CustomerAccountManager ()
	{
	}

    	/*
     	* Method used to create a Customer in the database
     	* @param customer Customer Object
     	* @return customerid Long
    	*/
	public Long createCustomer(Customer customer)
	{

	     Session session = HibernateUtil.getSessionFactory().getCurrentSession();
           session.beginTransaction();
	     session.save(customer);
           session.getTransaction().commit();
           return customer.getCustomerid();
	}

    	/*
     	* Method used to check Customer Login Details
     	* @param user String - username
     	* @return password String
    	*/
	public String checkCustomerLogin(String user)
	{
	     	String password = null;
	     	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      	session.beginTransaction();
		password = (String) session
                .createQuery("select password from Customer customer where customer.email = :email")
                 .setParameter("email", user)
			.uniqueResult();
		session.getTransaction().commit();
		return password;
	}

    	/*
     	* Method used to create a Account for a customer
     	* @param account Account Object
     	* @return accountid Long
    	*/
	public Long createAccount(Account account)
	{
	     Session session = HibernateUtil.getSessionFactory().getCurrentSession();
           session.beginTransaction();
	     session.save(account);
           session.getTransaction().commit();
           return account.getAccountid();
	}

	public String updateAccount(String payerEmail, String txnId, String payment)
	{
/*
	     Session session = HibernateUtil.getSessionFactory().getCurrentSession();
           session.beginTransaction();

	     Long accountId = session
                .createQuery()
                 .setParameter()
			.uniqueResult();

        	Account anAccount = (Account) session.load(Account.class, accountId);
		anAccount.setPayment(payment);
		anAccount.setTxnid(txnId);
	      session2.update(anAccount);

           session.getTransaction().commit();
*/
		/*To FIX*/
		return ("Y");
	}

    	/*
     	* Method used to list all accounts
     	* @return accounts List
    	*/
    	public List listAccounts() 
	{
      	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      	session.beginTransaction();
      	List result = session.createQuery("from Account").list();
	     	session.getTransaction().commit();
      	return result;
    	}

    	/*
     	* Method to link an account to a customer
     	* @param customerId - Long
     	* @param accountId - Long
    	*/
	public void addCustomerToAccount(Long customerId, Long accountId) 
	{

      	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      	session.beginTransaction();

      	Customer aCustomer = (Customer) session
                .createQuery("select c from Customer c left join fetch c.accounts where c.customerid = :cid")
                .setParameter("cid", customerId)
                .uniqueResult(); // Eager fetch the collection so we can use it detached

       	Account anAccount = (Account) session.load(Account.class, accountId);
	      session.getTransaction().commit();

	      // End of first unit of work
      	aCustomer.getAccounts().add(anAccount); // aCustomer is detached
        	// or bidirectional safety method, setting both sides: aCustomer.addToAccount(anAccount);

        	// Begin second unit of work
        	Session session2 = HibernateUtil.getSessionFactory().getCurrentSession();
        	session2.beginTransaction();
        	session2.update(aCustomer); // Reattachment of aCustomer
        	session2.getTransaction().commit();
    }

    	/*
     	* Method used to get Customer Details for an username from the database
     	* @param emailId String
     	* @return aCustomer Customer Object
    	*/
	public Customer getCustomer(String emailId)
	{

	      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      	session.beginTransaction();

		Customer aCustomer = (Customer) session
               .createQuery("select customer from Customer customer where customer.email = :email")
                .setParameter("email", emailId)
                .uniqueResult(); // Eager fetch the collection so we can use it detached

	     	session.getTransaction().commit();
		return aCustomer;
		
	}

    	/*
     	* Method used to delete a Customer from the database
     	* @param emailId String
    	*/
	public void deleteCustomer(String emailId)
	{

	      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      	session.beginTransaction();

		Customer aCustomer = (Customer) session
               .createQuery("select customer from Customer customer where customer.email = :email")
                .setParameter("email", emailId)
                .uniqueResult(); // Eager fetch the collection so we can use it detached

		session.delete(aCustomer);
	     	session.getTransaction().commit();
		
	}

    	/*
     	* Method used to update a Customer in the database
     	* @param newCustomer Customer
    	*/
	public void updateCustomer(Customer newCustomer)
	{

	      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      	session.beginTransaction();

		Customer aCustomer = (Customer) session
               .createQuery("select customer from Customer customer where customer.email = :email")
                .setParameter("email", newCustomer.getEmail())
                .uniqueResult(); // Eager fetch the collection so we can use it detached

		aCustomer.setFirstname(newCustomer.getFirstname());
		aCustomer.setLastname(newCustomer.getLastname());
		aCustomer.setAddress(newCustomer.getAddress());
		aCustomer.setCity(newCustomer.getCity());
		aCustomer.setCountry(newCustomer.getCountry());
		aCustomer.setPhone(newCustomer.getPhone());
		aCustomer.setCompany(newCustomer.getCompany());
		aCustomer.setPassword(newCustomer.getPassword());
		aCustomer.setTime(newCustomer.getTime());
		session.update(aCustomer);
	     	session.getTransaction().commit();
		
	}

    	/*
     	* Method used to update the password of a customer in the database
     	* @param newCustomer Customer
    	*/
	public void updatePassword(String emailid, String password)
	{

	      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      	session.beginTransaction();

		Customer aCustomer = (Customer) session
               .createQuery("select customer from Customer customer where customer.email = :email")
                .setParameter("email", emailid)
                .uniqueResult(); // Eager fetch the collection so we can use it detached

		aCustomer.setPassword(password);
		session.update(aCustomer);
	     	session.getTransaction().commit();
		
	}


}